package com.hsbc.dev.teamo4.sms.sm.dao.impl;

import com.google.common.collect.Lists;
import com.hsbc.dev.teamo4.sms.common.utils.data.DBUtil;
import com.hsbc.dev.teamo4.sms.sm.dao.IUserRoleDao;
import com.hsbc.dev.teamo4.sms.sm.po.Permission;
import com.hsbc.dev.teamo4.sms.sm.po.User;
import com.hsbc.dev.teamo4.sms.sm.po.UserRole;
import org.apache.commons.dbutils.handlers.ArrayHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.util.List;

/**
 * @author:Kevin
 * @version:
 * @Project: permission_manage
 * @Package: com.hsbc.dev.teamo4.sms.sm.dao.impl
 * @Description:
 * @Date date: 2018/7/30
 */
public class UserRoleDaoImpl implements IUserRoleDao {
    @Override
    public UserRole saveEntity(UserRole userRole) {
        String sql="insert into sm_user_role values(?,?,?,?,?,?,?,?)";
        return (UserRole) DBUtil.saveEntity(sql,userRole,null);
    }

    @Override
    public UserRole updateEntity(UserRole userRole) {
        String sql="update  sm_user_role set  userId=?,roleId=?,updateId=?,updateTime=?,isEnable=? where id=?";
        return (UserRole) DBUtil.updateEntity(sql,userRole, Lists.newArrayList("id","createId","createTime"),userRole.getId());

    }

    @Override
    public int updateByIsEnable(String id, Boolean bool) {
        String sql = "update sm_user_role set isEnable=? where id=?";
        return DBUtil.updateByIsEnable(sql,id,bool);
    }

    @Override
    public UserRole queryById(String id) {
        String sql="select * from sm_user_role where id='"+id+"'";
        BeanHandler beanHandler=new BeanHandler<>(UserRole.class);
        return (UserRole) DBUtil.queryById(sql,beanHandler);
    }

    @Override
    public List<UserRole> queryAllEntity() {
        String sql="select * from sm_user_role where isEnable='"+true+"'";
        BeanListHandler beanListHandler=new BeanListHandler<>(UserRole.class);
        return (List<UserRole>) DBUtil.queryAllEntity(sql,beanListHandler);
    }

    @Override
    public List<String> getRoleIdListByUserId(String userId) {
        String sql="select roleId from sm_user_role where userId='"+userId+"' and isEnable='"+true+"'";
        ArrayHandler arrayHandler=new ArrayHandler();
        return (List<String>) DBUtil.queryString(sql,arrayHandler);
    }
}
